package com.zfy.utils;

import java.sql.*;



public class DBManager {
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	private static final String URL = "jdbc:mysql://localhost:3306/zfy-mall?useUnicode=true&characterEncoding=UTF-8";
	private static final String USED = "root";
	private static final String PAD = "gy@147258369";

	public static Connection getConnection() {//
		try {
			Class.forName(DRIVER);
			return DriverManager.getConnection(URL, USED, PAD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void closeAll(Connection conn, Statement st, ResultSet rs) {//

		try {
			if (rs != null) {
				rs.close();
			}
			if (st != null) {
				st.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static int executeUpdate(String sql) {//
		Connection conn=null;
		Statement st=null;
		try {
			conn=getConnection();
			st=conn.createStatement();
			return st.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		} finally {
			closeAll(conn, st, null);
		}
	}
	public static QueryDataResult executeQuery(String sql) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			conn=getConnection();
			st=conn.createStatement();
			rs=st.executeQuery(sql);
			return new QueryDataResult(conn, st, rs);
		} catch (SQLException e) {
			e.printStackTrace();
			closeAll(conn, st, rs);
			return null;
		}
	}
}
